package edu.drexel.cs544.group11.autodiscover;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * SERVICE
 * 
 * Starts the autodiscovery server.
 * 
 * CS 544
 * 
 * @author Group 11:
 * @author Jason Setzer
 * @author Tam Thongsima
 * @author Kenny Oyefara
 * @author Chase Miller
 * 
 * @date Spring 2013
 * 
 */
public class StartAutoDiscovery {
    
    final static int autoport = 5944; // SERVICE
    
    public static void main(String[] args) throws Exception {
	if (args.length > 0) {
	    String IP = args[0];
	    DatagramPacket outPacket = null;
	    MulticastSocket socket = null;
	    byte[] buf = new byte[256];
	    try {
		//Prepare to join multicast group
		socket = new MulticastSocket(autoport);
		InetAddress address = InetAddress.getByName("225.3.3.4");
		socket.joinGroup(address);
		System.out.println("auto discovery service running...");
		
		String buildmessage = "BSP:" + IP + ":" + autoport;
		buf = buildmessage.getBytes();
		outPacket = new DatagramPacket(buf, buf.length, address, autoport);
		
		while (true) {
		    Thread.sleep(5000);
		    System.out.println("Srv Sending");
		    socket.send(outPacket);
		}
	    }
	    catch (IOException ioe) {
		System.out.println(ioe);
	    }
	}
	else {
	    System.out.println("Usage - please supply an ip address");
	}
    }
}
